Page({
  data: {
    user: ''
  },
  onShow() {
    // if (wx.getStorageSync("user")) return this.setData({
    //   user: wx.getStorageSync("user")
    // })
    this.getUser()
  },
  // 登录成功后获取用户信息
  getUser() {
    console.log('Bearer ' + wx.getStorageSync('token'))
    wx.request({
      url: 'http://ttapi.research.itcast.cn/app/v1_0/user',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res)
        if (res.statusCode !== 200) {
          wx.removeStorageSync('user')
          this.setData({
            user: ""
          })
          return
        }
        wx.setStorageSync('user', res.data.data)
        // this.data.user = wx.getStorageSync('user')
        this.setData({
          // user: wx.getStorageSync('user')
          user: res.data.data
        })
      }.bind(this)
    })
  },
  // 登录页
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  // 个人信息页
  toProfile() {
    wx.navigateTo({
      url: '/pages/profile/profile',
    })
  },
  // 去关注&粉丝页面
  toAtten(e) {
    wx.navigateTo({
      url: '/pages/attentions/attentions?i=' + e.currentTarget.dataset.i,
    })
  },
  // 去收藏/历史页面
  toHistory(e) {
    wx.navigateTo({
      url: '/pages/history/history?i=' + e.currentTarget.dataset.i,
    })
  },
  // 退出
  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出当前账号吗？',
      success: function (res) {
        if (res.confirm) {
          wx.clearStorageSync()
          this.getUser()
        } else if (res.cancel) {

        }
      }.bind(this)
    })
  },
  toxiaozhi() {
    wx.navigateTo({
      url: '/pages/chat_in/chat_in',
    })
  },
  goDynamic(){
    wx.switchTab({
      url: '/pages/dynamic/dynamic',
    })
  }
})